import sys
import pygame
from myClass import *
import time
from myFunctions import *
# 如果没有下列主循环代码，运行结果会一闪而过
if __name__ == "__main__":
	# 初始化
	pygame.init()
	pixelWidth=70
	rotateSpeed=pixelWidth/100
	marginWidth=0.25   #marginWidth=pixels/100
	Screen_H=7  #Y
	Screen_W=12  #X
	W_Height=Screen_H*pixelWidth
	W_Width = Screen_W * pixelWidth
	screen = pygame.display.set_mode((W_Width,W_Height))
	screen.fill("white")

	# for i in range(0, 12):
	# 	pygame.draw.line(screen, (0, 0, 0), [pixelWidth * i, 0], (pixelWidth * i, W_Height), 1)
	# 	pygame.draw.line(screen, (0, 0, 0), [0, pixelWidth * i], (W_Width, pixelWidth * i), 1)
	pygame.display.set_caption('stimulate')


	# 加载图片
	backGroundImg = pygame.image.load("./assets/Imgs/roadsWithGround.png")
	backGroundImg=pygame.transform.scale(backGroundImg,(12*pixelWidth,7*pixelWidth))
	# carInImg = pygame.image.load("./assets/Imgs/small_car_in.png")
	# carInImg=pygame.transform.scale(carInImg,(4*pixelWidth,2*pixelWidth))
	# carOutImg = pygame.image.load("./assets/Imgs/small_car_out.png")
	# carOutImg=pygame.transform.scale(carOutImg,(4*pixelWidth,2*pixelWidth))
	ChargeStationImg = pygame.image.load("./assets/Imgs/ChargeStation.png")
	ChargeStationImg=pygame.transform.scale(ChargeStationImg,(6*pixelWidth,4*pixelWidth))
	poleImg_in=pygame.image.load("./assets/Imgs/inPole.png")
	poleImg_in=pygame.transform.scale(poleImg_in,(4*pixelWidth,4*pixelWidth))
	poleImg_out = pygame.image.load("./assets/Imgs/outPole.png")
	poleImg_out=pygame.transform.scale(poleImg_out,(4*pixelWidth,4*pixelWidth))
	# bcarInImg=pygame.image.load("./assets/Imgs/big_car_in.png")
	# bcarInImg=pygame.transform.scale(bcarInImg,(4*pixelWidth,3*pixelWidth))
	# bcarOutImg=pygame.image.load("./assets/Imgs/big_car_out.png")
	# bcarOutImg=pygame.transform.scale(bcarOutImg,(4*pixelWidth,3*pixelWidth))

	# screen.blit(backGroundImg, (0, 1))
	# inPoints = [(-2 * pixelWidth, 5 * pixelWidth),
	# 			((-3 - 2 * marginWidth) * pixelWidth, (4 - 2 * marginWidth) * pixelWidth),
	# 			((12 - 2 * marginWidth) * pixelWidth, (-1 - 2 * marginWidth) * pixelWidth), (13 * pixelWidth, 0)]
	# outPoints = [(-1 * pixelWidth, 6 * pixelWidth),
	# 			 ((2 * marginWidth) * pixelWidth, (7 + 2 * marginWidth) * pixelWidth),
	# 			 ((15 + 2 * marginWidth) * pixelWidth, (2 + 2 * marginWidth) * pixelWidth),
	# 			 (14 * pixelWidth, pixelWidth)]
	# povilionPoint = [(4 * pixelWidth, 3 * pixelWidth), (7 * pixelWidth, 2 * pixelWidth),
	# 				 (8 * pixelWidth, 3 * pixelWidth), (5 * pixelWidth, 4 * pixelWidth)]
	#
	# pygame.draw.polygon(screen, (0, 0, 0), inPoints)
	# pygame.draw.polygon(screen, (0, 0, 0), outPoints)
	# pygame.draw.polygon(screen, (255, 255, 0), povilionPoint)
	speed = 1
	num = 0
	flag1 = True # 进杆子
	flag2 = True # 出杆子
	target = 1

	clock = pygame.time.Clock()
	theCarIn = 0  # 0为小车，#1为大车
	theCarOut = 2  # 2为小车，#3为大车
	inCarImage,inCarType=LoadCarInfo(theCarIn, pixelWidth)
	Car_in = Car(inCarType, marginWidth, pixelWidth, inCarImage)

	outCarImage,outCarType = LoadCarInfo(theCarOut, pixelWidth)
	Car_out = Car(outCarType, marginWidth, pixelWidth, outCarImage)

	enterCar = None
	isEnterFinish = 0
	enType=0   #0代表从收费处出去，1代表杆子抬起进入停车场，2代表出停车场

	outCar=None
	isOutFinish = 0
	outType = 0

	# 拦截杆参数设置
	Pole1Rotate=0
	Pole2Rotate=0

	flag1direction=-1
	flag2direction=1

	image_raw = poleImg_in
	image_raw1 = poleImg_out

	inangle = 0
	inPoleRotatedCenter=(8.1*pixelWidth,2.24*pixelWidth)
	InPole = pygame.transform.rotate(image_raw, 0)

	outangle=0
	outPoleRotatedCenter = (3.9*pixelWidth, 2.34*pixelWidth)
	OutPole = pygame.transform.rotate(image_raw1, 0)

#事件创建
	#进场动画事件
	IN_ANIME1_EVENT = pygame.USEREVENT+1
	IN_ANIME2_EVENT = pygame.USEREVENT+2
	IN_ANIME3_EVENT= pygame.USEREVENT+11
	OUT_ANIME1_EVENT = pygame.USEREVENT+3
	OUT_ANIME2_EVENT = pygame.USEREVENT+4
	OUT_ANIME3_EVENT=pygame.USEREVENT+12
	eventIn1=pygame.event.Event(IN_ANIME1_EVENT)
	eventIn2 = pygame.event.Event(IN_ANIME2_EVENT)
	eventIn3=pygame.event.Event(IN_ANIME3_EVENT)
	eventOut1 = pygame.event.Event(OUT_ANIME1_EVENT)
	eventOut2 = pygame.event.Event(OUT_ANIME2_EVENT)
	eventOut3=  pygame.event.Event(OUT_ANIME3_EVENT)

	setflag1ToFalse = pygame.USEREVENT + 5
	setPole1RotateTo0 = pygame.USEREVENT + 6
	pygame.time.set_timer(setPole1RotateTo0,0)
	pygame.time.set_timer(setflag1ToFalse, 0)

	setflag2ToFalse = pygame.USEREVENT + 7
	setPole2RotateTo0 = pygame.USEREVENT + 8
	pygame.time.set_timer(setPole2RotateTo0, 0)
	pygame.time.set_timer(setflag2ToFalse, 0)

	# in抬杆动画事件
	# out抬杆动画事件
	inPoleAnime = pygame.USEREVENT + 9
	outPoleAnime= pygame.USEREVENT + 10
	inPoleAnimeEvent = pygame.event.Event(inPoleAnime)
	outPoleAnimeEvent = pygame.event.Event(outPoleAnime)
	#进出车辆选择
	SMALL_CAR_ENTER=pygame.USEREVENT + 13
	BIG_CAR_ENTER = pygame.USEREVENT + 14
	SMALL_CAR_OUT = pygame.USEREVENT + 15
	BIG_CAR_OUT = pygame.USEREVENT + 16
	SCE_EVENT=pygame.event.Event(SMALL_CAR_ENTER)
	BCE_EVENT=pygame.event.Event(BIG_CAR_ENTER)
	SCO_EVENT = pygame.event.Event(SMALL_CAR_OUT)
	BCO_EVENT = pygame.event.Event(BIG_CAR_OUT)



#拦截杆自动原理
# Pole1Rotate是启用旋转
# flag1=True,Pole1Rotate=1 抬杆
# flag1=false,Pole1Rotate=1下杆
# 抬杆动画
# flag1=True,Pole1Rotate=1 抬杆
# setTimer（设置flag1为false的事件，2000）
# 设置flag1为false的事件:（flag1=false,setTimeer(设置Pole1Rotate为0的事件,1000)，setTimer（设置flag1为false的事件，0））
# 设置Pole1Rotate为0的事件（Pole1Rotate=0，setTimer（设置Pole1Rotate为0的事件，0）


	while True:
		clock.tick(60)

		# 进车辆
		# 进小车
		# pygame.event.post(SCE_EVENT)
		# 进大车
		# pygame.event.post(BCE_EVENT)
		# 出车辆
		# 出小车
		# pygame.event.post(SCO_EVENT)
		# 出大车
		# pygame.event.post(BCO_EVENT)
		cartype = open('''C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\cartype.txt''', "r")
		carcontent = cartype.read()
		cartype.close()
		if carcontent == "0":
			pygame.event.post(SCE_EVENT)
			pygame.event.post(SCO_EVENT)
		elif carcontent == "1":
			pygame.event.post(BCE_EVENT)
			pygame.event.post(BCO_EVENT)

		# 从外进入车库动画：
		# 		  1.从外到识别区域
		# 		  pygame.event.post(eventIn1)
		# 		  2.从识别区域到车库内
		# 		  pygame.event.post(eventIn2)
		# 		  3.倒车
		# 		  pygame.event.post(eventIn3)
		# 	      4.独立的抬杆动画
		# 	      pygame.event.post(inPoleAnimeEvent)
		#
		#       从车库出去所含动画：
		# 		  1.从车库到识别区域
		# 		  pygame.event.post(eventOut1)
		# 		  2.从识别区域到外
		# 		  pygame.event.post(eventOut2)
		# 		  3.倒车
		# 		  pygame.event.post(eventOut3)
		# 		  4.独立的抬杆动画
		# 		  pygame.event.post(outPoleAnimeEvent)
		f = open("C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\carcome.txt", "r")
		content = f.read()
		f.close()
		if content == "1":
			pygame.event.post(eventIn1)
			fw = open("C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\carcome.txt", "w")
			fw.write("0")
			fw.close()
		elif content == "2":
			pygame.event.post(eventIn2)
			fw = open("C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\carcome.txt", "w")
			fw.write("0")
			fw.close()
		elif content == "-1":
			pygame.event.post(eventIn3)
			fw = open("C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\carcome.txt", "w")
			fw.write("0")
			fw.close()

		f = open("C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\carout.txt", "r")
		content = f.read()
		f.close()
		if content == "1":
			pygame.event.post(eventOut1)
			fw = open("C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\carout.txt", "w")
			fw.write("0")
			fw.close()
		elif content == "2":
			pygame.event.post(eventOut2)
			fw = open("C:\\Users\\戴尔\\Desktop\\停车场系统\\图像处理\\图形软件\\chepai\\carout.txt", "w")
			fw.write("0")
			fw.close()
		#elif content == "-1":
			#pygame.event.post(eventOut3)
			#fw = open("D:\学习\大四\图像处理\图形软件\chepai\carout.txt", "w")
			#fw.write("0")
			#fw.close()


		if Pole1Rotate==1:
			if flag1 == True:
				if inangle <= 90:
					flag1direction = 1/rotateSpeed
					flag1, inangle, flag1direction, InPole = flagRotate(flag1, inangle, flag1direction, InPole,
																		 image_raw)


			if flag1 == False:
				if inangle >= 0:
					flag1direction = -1/rotateSpeed
					flag1, inangle, flag1direction, InPole = flagRotate(flag1, inangle, flag1direction, InPole,
																		 image_raw)

		if Pole2Rotate==1:
			if flag2 == True:
				if outangle >= -45:
					flag2direction = -1/rotateSpeed
					flag2, outangle, flag2direction, OutPole = flagRotate(flag2, outangle, flag2direction, OutPole,
																		 image_raw1)

			if flag2 == False:
				if outangle <= 0:
					flag2direction = 1/rotateSpeed
					flag2, outangle, flag2direction, OutPole = flagRotate(flag2, outangle, flag2direction, OutPole,
																		 image_raw1)


		if enterCar != None:
			if(isEnterFinish!=1):
				enterCar, isEnterFinish = CarMove(enterCar, 1, 1, enType, Car_in.getinCheckPoint(), isEnterFinish,pixelWidth)


		if outCar != None:
			if(isOutFinish!=1):
				outCar, isOutFinish = CarMove(outCar, -1, 1, outType, Car_out.getoutCheckPoint(), isOutFinish,pixelWidth)


		# 循环获取事件，监听事件
		for event in pygame.event.get():
			# 判断用户是否点了关闭按钮
			if event.type == pygame.QUIT:
				# 卸载所有模块
				pygame.quit()
				# 终止程序
				sys.exit()
			# 进入收费位放行
			if event.type == IN_ANIME1_EVENT:
				enterCar = None
				tempaa = Car_in.getentrancePoint()
				enterCar = pygame.Rect(tempaa.x, tempaa.y, 4*pixelWidth, 2*pixelWidth)
				enType = 0
				isEnterFinish = 0
				# print(enterCar)
				# print(enType)
				# 抬杆放行
			if event.type == IN_ANIME2_EVENT:
				if (isEnterFinish == 1):
					#抬杆动画控制
					pygame.event.post(inPoleAnimeEvent)
					# flag1=True,Pole1Rotate=1 抬杆
					# setTimer（设置flag1为false的事件，2000）

					enterCar = None
					isEnterFinish = 0
					enType = 1
					tempaa = Car_in.getinCheckPoint()
					enterCar = pygame.Rect(tempaa.x, tempaa.y, 4*pixelWidth, 2*pixelWidth)
					# print(enterCar)
			if event.type == IN_ANIME3_EVENT:
				if (isEnterFinish == 1):

					enterCar = None
					isEnterFinish = 0
					enType = 2
					tempaa = Car_in.getinCheckPoint()
					enterCar = pygame.Rect(tempaa.x, tempaa.y, 4*pixelWidth, 2*pixelWidth)
					# print(enterCar)
			if event.type == OUT_ANIME1_EVENT:
				outCar = None
				tempaa = Car_out.getoutPoint()
				outCar = pygame.Rect(tempaa.x, tempaa.y, 4*pixelWidth, 2*pixelWidth)
				outType = 0
				isOutFinish = 0

				# print(outCar)
				# print(outType)
			if event.type == OUT_ANIME2_EVENT:
				if (isOutFinish == 1):

					pygame.event.post(outPoleAnimeEvent)
					# flag2=True,Pole2Rotate=1 抬杆
					# setTimer（设置flag2为false的事件，2000）

					outCar = None
					isOutFinish = 0
					outType = 1
					tempaa = Car_out.getoutCheckPoint()
					outCar = pygame.Rect(tempaa.x, tempaa.y,4*pixelWidth, 2*pixelWidth)
					# print(outCar)
					# print(outType)
			if event.type == OUT_ANIME3_EVENT:
				if (isOutFinish == 1):
					outCar = None
					isOutFinish = 0
					outType = 2
					tempaa = Car_out.getoutCheckPoint()
					outCar = pygame.Rect(tempaa.x, tempaa.y, 4*pixelWidth, 2*pixelWidth)
					# print(outCar)
					# print(outType)



			# flag1=True,Pole1Rotate=1 抬杆
			# setTimer（设置flag1为false的事件，2000）
			# 设置flag1为false的事件:（flag1=false,setTimeer(设置Pole1Rotate为0的事件,1000)，setTimer（设置flag1为false的事件，0））
			# 设置Pole1Rotate为0的事件（Pole1Rotate=0，setTimer（设置Pole1Rotate为0的事件，0）
			if event.type == setflag1ToFalse:
				flag1 = False
				pygame.time.set_timer(setPole1RotateTo0, 35*pixelWidth)
				pygame.time.set_timer(setflag1ToFalse, 0)

			if event.type == setPole1RotateTo0:
				Pole1Rotate = 0
				pygame.time.set_timer(setPole1RotateTo0, 0)

			if event.type ==inPoleAnime:
				flag1 = True
				Pole1Rotate = 1
				pygame.time.set_timer(setflag1ToFalse, 35*pixelWidth)

			# 同理
			if event.type == setflag2ToFalse:
				flag2 = False
				pygame.time.set_timer(setPole2RotateTo0, 2000)
				pygame.time.set_timer(setflag2ToFalse, 0)

			if event.type == setPole2RotateTo0:
				Pole2Rotate = 0
				pygame.time.set_timer(setPole2RotateTo0, 0)

			if event.type==outPoleAnime:
				flag2 = True
				Pole2Rotate = 1
				pygame.time.set_timer(setflag2ToFalse, 2000)





			if event.type == SMALL_CAR_ENTER:
				theCarIn = 0
				inCarImage, inCarType = LoadCarInfo(theCarIn, pixelWidth)
				Car_in = Car(inCarType, marginWidth, pixelWidth, inCarImage)

			if event.type ==SMALL_CAR_OUT:
				theCarOut = 2
				outCarImage, outCarType = LoadCarInfo(theCarOut, pixelWidth)
				Car_out = Car(outCarType, marginWidth, pixelWidth, outCarImage)

			# 同理
			if event.type == BIG_CAR_ENTER:
				theCarIn = 1
				inCarImage, inCarType = LoadCarInfo(theCarIn, pixelWidth)
				Car_in = Car(inCarType, marginWidth, pixelWidth, inCarImage)

			if event.type ==  BIG_CAR_OUT:
				theCarOut = 3
				outCarImage, outCarType = LoadCarInfo(theCarOut, pixelWidth)
				Car_out = Car(outCarType, marginWidth, pixelWidth, outCarImage)


			# 键盘控制
			if event.type == pygame.KEYDOWN:
				# 进去的控制
				#倒车
				if event.key == pygame.K_a:
					pygame.event.post(eventIn3)

				# 进入
				if event.key == pygame.K_s:
					pygame.event.post(eventIn1)

				# 检测抬杆
				if event.key == pygame.K_d:
					pygame.event.post(eventIn2)

				#出去的控制
				# 倒车
				if event.key == pygame.K_z:
					pygame.event.post(eventOut3)

				# 进入
				if event.key == pygame.K_x:
					pygame.event.post(eventOut1)

				#检测抬杆
				if event.key == pygame.K_c:
					pygame.event.post(eventOut2)

				#独立的抬杆动画：进入时
				if event.key == pygame.K_h:
					pygame.event.post(SCE_EVENT)
				# 0为小车，#1为大车

				# 独立的抬杆动画：出去时时
				if event.key == pygame.K_j:
					pygame.event.post(BCE_EVENT)

				if event.key == pygame.K_n:
					pygame.event.post(SCO_EVENT)
				# 独立的抬杆动画：出去时时
				if event.key == pygame.K_m:
					pygame.event.post(BCO_EVENT)

#

	#选择车辆进出的种类

		screen.blit(backGroundImg,(0,1))

		if outCar!=None:
			screen.blit(Car_out.getCarImg(), outCar)

		screen.blit(ChargeStationImg, (3*pixelWidth,0))


		screen.blit(InPole, InPole.get_rect(center=tuple(inPoleRotatedCenter)))

		if enterCar!=None:
			screen.blit(Car_in.getCarImg(), enterCar)

		screen.blit(OutPole, OutPole.get_rect(center=tuple(outPoleRotatedCenter)))
		# 更新屏幕内容
		pygame.display.update()



